import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;

import com.masc.gurr.model.Contact;
import com.masc.gurr.model.Group;
import com.masc.gurr.model.ProtocolType;


public class GroupTest {

	Group g;
	Group subg1;
	Group subg2;
	Contact c1;
	Contact c2;
	Contact c3;
	
	@Before
	public void setUp() throws Exception {
		g = new Group("/");
		g.setGid(0);
		subg1 = new Group("buddies");
		subg1.setGid(1);
		subg1.setParentGroup(g);
		g.addSubGroup(subg1);
		c1 = new Contact("123","Alice", ProtocolType.ICQ, "it's me");
		subg1.addContact(c1);
		c2 = new Contact("456","Bob", ProtocolType.ICQ, "hey");
		subg1.addContact(c2);

		subg2 = new Group("not in list");
		subg2.setGid(2);
		subg2.setParentGroup(g);
		g.addSubGroup(subg2);
		c3 = new Contact("789","Hans", ProtocolType.ICQ, "off");
		subg2.addContact(c3);
	}
	
	@Test
	public void testGetName() {
		assertEquals("/", g.getName());
	}

	@Test
	public void testSetName() {
		g.setName("newname");
		assertEquals("newname", g.getName());
	}

	@Test
	public void testGetContacts() {
		Group sg = g.getSubgroups().next();
		Iterator<Contact> it = sg.getContacts();
		assertEquals(c1, it.next());
		assertEquals(c2, it.next());
	}

	@Test
	public void testSetContacts() {
		ArrayList<Contact> cl = new ArrayList<Contact>();
		Contact c = new Contact("999", ProtocolType.ICQ);
		cl.add(c);
		subg2.setContacts(cl);
		assertTrue(subg2.getContacts().next().equals(c));
	}

	@Test
	public void testGetSubgroups() {
		assertTrue(g.getSubgroups().next().equals(subg1));
	}

	@Test
	public void testRemoveSubGroup() {
		g.removeSubGroup(subg1);
		Iterator<Group> itg = g.getSubgroups();
		
		/* removed reference from g to subgroup */
		while (itg.hasNext()) {
			assertFalse(itg.next().equals(subg1));
		}
	
		/* removed reference from subgroup to g */
		assertTrue(subg1.getParentGroup() == null);
		
		/* all contacts of subg1 are no longer referenced in g's map */
		Iterator<Contact> itc = subg1.getContacts();
		while (itc.hasNext()) {
			Contact c = itc.next();
			assertFalse(g.getMap().containsKey(c.getUin()));
		}
	}
//
//	@Test
//	public void testAddSubGroup() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testGetMap() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testAddContact() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testRemoveContactFromList() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testRemoveContact() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testGetParentGroup() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testSetParentGroup() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testGetGid() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testSetGid() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testRemoveAll() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testAddContactToMap() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testRemoveContactFromMap() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testGetChildContact() {
//		fail("Not yet implemented");
//	}

}
